<template>
    <div class="pd-20">
        <el-form :model="form" label-width="80px" ref="form">
            <el-form-item label="名称">
                <el-input placeholder="请输入名称" style="width:500px" v-model="form.name"></el-input>
            </el-form-item>


            <el-form-item label="描述">
                <el-input placeholder="请输入描述" style="width:500px" v-model="form.desc"></el-input>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" style="width: 470px;margin: 30px auto;" @click="submit">提交</el-button>
            </el-form-item>





        </el-form>
    </div>
</template>
<script>
import medium from '@/components/medium/index'
import VueEditor from '@/components/Editor/index' // ES6 Module
export default {
    components: {
        medium,
        VueEditor,
    },
    data() {
        return {
            form: {
                name: '',
                desc: '',
            },
            type: 'create',//判断状态
        }
    },
    mounted() {
        if (this.$route.query.id) {
            this.type = 'update'
            this.getAdministratorsRole(this.$route.query.id)
        }
    },
    methods: {
        //根据id获取管理员角色
        getAdministratorsRole(id) {
            this.$request.get('/admin-role/get/' + id,).then((res) => {
                this.form = res
                console.log("res.data", res)
                // this.init=true
            })
        },
        toAdministrators() {
            this.$router.push({ path: 'roleList' })
        },


        submit() {
            if (!(this.form.name && this.form.desc)) {
                this.$message({
                    message: '请将信息填写完整！',
                    type: 'warning'
                });
                return
            }

            if(this.type == 'create'){
                this.$request.post('/admin-role/create', this.form).then((res) => {
                this.$message({
                    message: '添加成功',
                    type: 'success'
                });
                this.toAdministrators()
            })
            }else{
                this.$request.put('/admin-role/update/'+ this.$route.query.id, this.form).then((res) => {
                this.$message({
                    message: '修改成功',
                    type: 'success'
                });
                this.toAdministrators()
            })
            }
           
        },




    },
}
</script>
<style lang="scss" scoped></style>